package com.allwees.bs.module.scm.req.query;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.allwees.bs.core.modelbase.req.QueryReq;
import com.allwees.bs.module.core.req.BranchUserQuery;
import com.allwees.bs.module.scm.constant.ELogisticsType;
import com.allwees.bs.module.scm.entity.ScmAllocateOrderSku;
import com.allwees.core.common.constant.EStatus;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * scm 待分配订单 : sku 维度
 *
 * @author mouhn
 * @since 2020-1-2 10:28
 */
@Data
public class ScmShippingOrderQueryReq extends QueryReq<ScmAllocateOrderSku> implements BranchUserQuery<ScmAllocateOrderSku> {

    private EScmShippingOrderQueryStatus status;

    private ELogisticsType logisticsType;

    private Date shippingAtFrom;
    private Date shippingAtTo;

    @Override
    public void buildQuery(QueryWrapper<ScmAllocateOrderSku> queryWrapper) {
        queryWrapper.and(StringUtils.isNotBlank(q), wrapper -> {
            wrapper.eq("uuid", q)
                    .or().eq("no", q);
        });
        buildBranchUserQuery(queryWrapper)
                .in("status", status.entityStatus())
                .ge(shippingAtFrom != null, "shipping_at", shippingAtFrom)
                .le(shippingAtTo != null, "shipping_at", shippingAtTo);
    }


    private enum EScmShippingOrderQueryStatus {
        // 所有、待入仓、部分入仓、全部入仓、退货
        ALL {
            @Override
            List<Integer> entityStatus() {
                return Arrays.asList(
                        EStatus.SCM_WAIT_ENTER.value(),
                        EStatus.SCM_ENTERING.value(),
                        EStatus.REFUND.value(),
                        EStatus.NORMAL.value()
                );
            }
        },
        WAIT_ENTER_WAREHOUSE {
            @Override
            List<Integer> entityStatus() {
                return Arrays.asList(
                        EStatus.SCM_WAIT_ENTER.value()
                );
            }
        },
        ENTERING_WAREHOUSE {
            @Override
            List<Integer> entityStatus() {
                return Arrays.asList(
                        EStatus.SCM_ENTERING.value()
                );
            }
        },
        ENTERED_WAREHOUSE {
            @Override
            List<Integer> entityStatus() {
                return Arrays.asList(EStatus.SCM_ENTERED.value());
            }
        },
        REFUND {
            @Override
            List<Integer> entityStatus() {
                return Arrays.asList(
                        EStatus.REFUND.value()
                );
            }
        },
        ;

        abstract List<Integer> entityStatus();
    }
}
